package com.getan.boot.module.system.controller;

import com.getan.boot.common.Pagination;
import com.getan.boot.common.ResponseBuilder;
import com.getan.boot.common.ResponseCodeData;
import com.getan.boot.common.annotation.OperateLog;
import com.getan.boot.common.domain.bo.IdBO;
import com.getan.boot.common.domain.bo.IdsBO;
import com.getan.boot.jdbc.customquery.CustomQueryBO;
import com.getan.boot.module.system.domain.bo.UserBO;
import com.getan.boot.module.system.entity.UserEntity;
import com.getan.boot.module.system.service.persistent.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Set;

/**
 * 用户管理
 *
 * @author Bruce.Gong
 * @since 1.0.0-SNAPSHOT
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/user")
public class UserController {

    private final String MODULE = "用户管理";
    private final UserService userService;

    @PostMapping("search")
    public ResponseCodeData<Pagination<UserEntity>> search(@RequestBody @Valid CustomQueryBO bo) {
        return ResponseBuilder.ok(userService.page(bo));
    }

    @OperateLog(module = MODULE, businessType = "ADD")
    @PostMapping("add")
    public ResponseCodeData<Long> add(@RequestBody @Valid UserBO.AddBO bo) {
        UserEntity entity = new UserEntity();
        BeanUtils.copyProperties(bo, entity);
        userService.checkUsername(entity);
        userService.checkMobile(entity);
        userService.addUser(entity, bo.getDeptIds(), bo.getRoleIds());
        return ResponseBuilder.ok(entity.getId());
    }

    @OperateLog(module = MODULE, businessType = "UPDATE")
    @PostMapping("update")
    public ResponseCodeData<Long> update(@RequestBody @Valid UserBO.UpdateBO bo) {
        UserEntity entity = new UserEntity();
        BeanUtils.copyProperties(bo, entity);
        userService.canUpdate(entity);
        userService.updateUser(entity, bo.getDeptIds(), bo.getRoleIds());
        return ResponseBuilder.ok(entity.getId());
    }

    @OperateLog(module = MODULE, businessType = "DELETE")
    @PostMapping("delete")
    public ResponseCodeData<Void> delete(@RequestBody @Valid IdsBO bo) {
        userService.canDelete(bo.getIds());
        userService.deleteUser(bo.getIds());
        return ResponseBuilder.ok();
    }

    @OperateLog(module = MODULE, businessType = "DISABLE")
    @PostMapping("disable")
    public ResponseCodeData<Void> disable(@RequestBody @Valid IdsBO bo) {
        userService.canDisable(bo.getIds());
        userService.disable(bo.getIds());
        return ResponseBuilder.ok();
    }

    @OperateLog(module = MODULE, businessType = "ENABLE")
    @PostMapping("enable")
    public ResponseCodeData<Void> enable(@RequestBody @Valid IdsBO bo) {
        userService.canEnable(bo.getIds());
        userService.enable(bo.getIds());
        return ResponseBuilder.ok();
    }

    @PostMapping("roleIds")
    public ResponseCodeData<Set<Long>> roleIds(@RequestBody @Valid IdBO bo) {
        return ResponseBuilder.ok(userService.roleIds(bo.getId()));
    }

}
